﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#ifndef MULTIPLEBURNDLG_H
#define MULTIPLEBURNDLG_H

#include <QMainWindow>
#include "ui_MultipleBurn.h"
#include "MultipleBurnDlgForOne.h"
#include "DataConvertor.h"
#include "CheckCom.h"
#include "RMDA_TIME_STRU.h"

namespace BurnDlg {

class MultipleBurnDlg : public QMainWindow {
    Q_OBJECT
public:
    enum class FactionFsm {
        FACTION_NONE,
        FACTION_WAITING,
        FACTION_DOING,
        FACTION_FINAL,
    };
    explicit MultipleBurnDlg(QWidget *parent = nullptr);
    ~MultipleBurnDlg() override;

    void SetOneLineNum(int num);
    void SetCtrlNum(int num);
    void InitUi();
    void SetChipType(ChipType chipTypeNeedSet);
    void SetAutoBurnFlag();
    void SetReopenComFlag(bool reopenComFlag);
    void SetBootType(BootType type);
    void SetEraseAll(int eraseMode);
    void SetSecConn(bool isSecConnNeedSet);
    void SetPeriod(int periodNeedSet);
    void UpdateComSetting(UINT baudNeedUpdate, UINT databitsNeedUpdate,
                          UINT stopbitsNeedUpdate, QSerialPort::Parity parityNeedUpdate,
                          UINT flowctrlNeedUpdate);
    void SetForceReadTime(UINT forceReadTimeNeedSet);
    void SetInfoArray(const QMap<int, BIN_INFO> &infoArray);
    void SetInfoArray(const QString &filePath);
    void SetForceReadFlag(bool isForceRead);
    void SetConfirmConnectSleepTime(UINT time);
    void SetConfirmConnectWaitTime(UINT time);
    void SetIndenpendentBurn(bool indenpendentNeedSet);
    void SetIsResetAfterSuccess(bool isResetAfterSuccessNeedSet);
    void SetYModemEachPackageSize(unsigned int modemPacketSizeBackupNeedSet);
    void SetIsEncrypt(bool isEncryptNeedSet);
    void SetEncryptBuffer(ENCRYPT_BUFFER encryptBufferNeedSet);
    void SetIsSwitchAfterLoader(bool isSwitchAfterLoaderNeedSet);
    void SetUsbDeviceInfo(HidDeviceInfo usbDeviceInfo);
    void SetUsbLocations(QStringList usbLocations);
    void SetMcuConfig(McuConfig config);
    void SetJtagSwd(McuJtagSwd config);
    void SetTransmissionMode(int tarns);
    void SetChipId(McuChipId id);
    void SetChipModel(int chipModelNeedSet);
    void SetInfoStartAddress(ulong startAddr);
    void SetMainStartAddress(MainStartAddrInfo startAddrInfo);
    void SetSramAddrInfo(SramAddrInfo sainfo);
    void SetIsReadBack(bool readBack);
    void SetIsRun(bool run);
    void SetLoaderInfo(QString loaderFileNeedSet, int loaderSizeNeedSet);
    void SetParameterSaveInfo(const QString &filePath, ulong baseNum);
protected:
    void closeEvent(QCloseEvent *event) override; // 关闭窗口会先处理该事件函数
    void LoadConfig();
    void RefreshAllComList();
    void InitMultipleParam(MultipleBurnDlgForOne &multiple, bool useExactTimer);
    void HandleFsmNone(LPARAM lParam);
    void HandleFsmWaiting(LPARAM lParam);
    void HandleFsmDoing(LPARAM lParam);
    bool JudgeBurnFail() const;
    bool JudgeFinish() const;
    void TimerConsumedStart();
    void HandleFsmFinal(LPARAM lParam);
    void HandleFsmFinalSplitCode();
    bool JudgeGoToWaiting() const;
    void TimerConsumedStop();
signals:
    void SignalClose();
    void SignalSaveDlgConfig();
    void SignalPostMessage(unsigned int, WPARAM srcData, LPARAM srcLen);
    void SignalUpdateList();
protected slots:
    void SlotSaveConfigAction();
    void SlotCheckComAction();
    void SlotRefreshComAction() const;
    void SlotCheckComTimeOut();
    void SlotConfigTimerTimeOut();
    void SlotBtnClickedButtonOneKeyConnect();
    void SlotNotifyState(unsigned int ret, unsigned int progress);
    void SlotConsumedTimeOut();
    void SlotBnClickedButtonOneKeyDisconnect();

private:
    Ui::MultipleBurn *ui;
    int xNum;
    QVector<MultipleBurnDlgForOne *> vecMultipleBurnDlg;
    unsigned int burnCtrlNum;
    QAction *saveConfigAction;
    QAction *checkComAction;
    QAction *refreshComAction;
    CheckCom checkCom;
    QTimer *checkComTimer;
    QTimer *configTimer;
    MyThreadClass::MyThread *checkComthread;

    ChipType chipType;
    bool isAuto;
    bool isReopenCom;
    BootType bootType;
    int isEraseMode;
    bool isSecConn;
    int period;
    UINT baud;
    UINT databits;
    UINT stopbits;
    QSerialPort::Parity parity;
    UINT flowctrl;
    UINT forceReadTime;
    bool isResetAfterSuccess;
    unsigned int modemPacketSizeBackup;
    bool isSwitchAfterLoader;
    QMap<int, BIN_INFO> binInfoArray;
    bool isForceReadWithSleep;
    UINT connectConfirmSleepTimeout;
    UINT connectConfirmWaitTimeout;
    bool indenpendent;
    QString strFilePath;
    unsigned int totalTime;
    unsigned int successTime;
    unsigned int failTime ;
    unsigned int curWaitTime;
    unsigned int curDoingTime;
    unsigned int curSuccessTime;
    unsigned int curFailTime;
    unsigned int curFinalToWaitTime;
    FactionFsm fsm;
    RMDA_TIME_STRU startTime;
    QTimer *timerConsumed;
    bool isEncrypt;
    ENCRYPT_BUFFER encryptBuffer;
    HidDeviceInfo deviceInfo;
    MyStringClass::StringClass stringClass;
    McuConfig mcuConfig;
    McuJtagSwd jtagSwd;
    int transmissionMode;
    McuChipId chipId;
    int chipModel;
    ulong infoStartAddress;
    MainStartAddrInfo mainStartAddrInfo;
    SramAddrInfo sramAddrInfo;
    bool isReadBack;
    bool isRun;
    QString loaderFile;
    int loaderSize;
    QString parameterFilePath;
    int eraseBaseNum;
};

}
#endif // MULTIPLEBURNDLG_H
